package com.huiminpay.transaction.Controller;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradeWapPayRequest;
import com.alipay.api.response.AlipayTradeWapPayResponse;

import com.huiminpay.merchant.common.cache.util.QRCodeUtil;
import lombok.SneakyThrows;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@RestController
public class PayTestController {
    //阿里支付网关
    private String serverUrl = "https://openapi-sandbox.dl.alipaydev.com/gateway.do";
    //应用id
    private String appId = "9021000155638919";
    //应用私钥
    private String privateKey = "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQC8eZJWyh6jq673Y4eI0TTDjw/0DDKQ6zXkyMz8XDiD1+EPlf4oVLscXSa4OzF7ohxSvhGTfMTVOgfpgwE3t+YQ1oGG5ivX0OqH+FptJuZggF18QV5WTchn4KKSp6kJVqhax07sjgOVOFZDWbqi6TTpLoGwQe7Be/24fyf0P9IW4ySNNigk+xctqtre/LmfJNbfxTcB/UYa8aIBYLM+gDf7GJEA4BQwR+5Qe5YD4os3dOoOu1lkC3jCgTbnGs1e+oy31UiRY+dLMJHMCMsifBDV21CSkInzJorwWyNXS+NC4BOUr1bz04Rms+Gv++mAclBcMdFMhiidDa4LdxQcU6D9AgMBAAECggEAGXZ7NMp9tTL+omGJ8AwDiuALEbeMJVQBpfUS7I0w/W7u8wIaoixAUyaqkn08xC00kmEnUruqg/whEYUnZvWrWV8BhM4X4kJRTSlaXiDBQwxUlPLn4dUEXDRXAmUwSxiwCgOfQAo3pbNExDidJGFJSq+DyeI+dVlg/8n+BduRcJHoBFxxTftSnk5vwA2uI3iqmCzLL9S/BlZHqzqYtATs2BabRl6zcpxViNhgWGT2XBhE+Z9BbnpOYqphMpmGT9K7IpBW0UJvJ0Ip89KNzHFrFudYetY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ROv0L4Olnob/J4AW7efiWmdTvU9Geh9NRXu7NwOckjRbd0/NkcF93aIvAOx9DqwbBtHQpZOw6rCArYpoq0r8CgYBIVAFojmvTwAevSnkR/OGZd4monCpd3CD9NVBTapfiYchndhjCYc2C4Kpm0Un5GXMQ79u7Njkya0r1XWn0YOCSDbCuiFXYRNkxg3ApFyC22NTzK8cH5xGu3fHVeOTN5Y985vqSSdLh+MyTKt1GTt1/A8S6tJA3es+AJDhxo2pCZQ==";
    private String format="json"; //格式类型
    private String charset="utf-8"; //编码集
    private String alipayPublicKey = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAkkMF0fKA0FFu8WwFfEsTi2lmh2vY9+Cd2vNeVOg2m3xnTbyy+w/0VzvaiXSyyVIa2VgLHskP7QAnM4VYO+awmgTfFt8bqVzdozWuHAc2FiwKLQsTaFe21ycOYAWPKcaj98zsvksQIhPhxvDRsJEkNdUwalSYSw1wZFaGk97bACyQTYwB9mau7ub2pMWEUR+KEGrbjkcbWmB7eoB6RXLHFqjTJNXgqNNu/Ney+fZ4YYgD2SssI+OIwSG3JLA8m+kl2pslzCvnDzIO6g147R0Ga4wxuUrbw0lLsTmllgkjEBBP2RtHFCZkhq5yqjjNzRrkdh4K0Q+heA4qB5eODrCMBwIDAQAB/LwFFD4oX2kRrW5lUZvyIeCJknAg7avuZvshcZI+ZdK0YwKyAPtUhm3TR9sA2/HXmKL356bcGuuDj1420ENInbmvL5T28d+LqAISW1Mjx21w98wYWXDHgyFHRyHcFndBCNpRR9oFImad+blpMBRcwKLp0AX/MwbkJKNIvmafgM6XgHcK4mkKex7U9MvN9WC+l8JGiVEPsMqDmGa1RuzSGMkOKpTP1canUhtxlbCAWT68ZpkkBWEu5wu4jpuIjoVLjt2wv6PdrwKLkYErChl6P860XdsjLBDR+y86ScLolaqyOe0XncY57VQ8+fJmKQLmgeqVkQyxFVr4WfwIDAQAB";//阿里公钥
    //签名类型
    private String signType="RSA2";

    @GetMapping("/alipaytest")
    public void alipaytest(HttpServletRequest request, HttpServletResponse response) throws Exception {
        //1.创建AlipayClient对象
        // DefaultAlipayClient(String serverUrl, String appId, String privateKey, String format, String charset, String alipayPublicKey, String signType)
        AlipayClient alipayClient = new DefaultAlipayClient(serverUrl,appId,privateKey,format,charset,alipayPublicKey,signType);
        //2.创建支付请求对象
        AlipayTradeWapPayRequest payRequest = new AlipayTradeWapPayRequest();
        payRequest.setBizContent("{" +
                " \"out_trade_no\":\"20250320010101123\"," +
                " \"total_amount\":\"6000\"," +
                " \"subject\":\"Iphone17 256G\"," +
                " \"product_code\":\"QUICK_WAP_PAY\"" +
                " }");
        //3.调用支付接口
        String body = null;
        try {
            AlipayTradeWapPayResponse payResponse = alipayClient.pageExecute(payRequest);
            //4.获取响应数据
            body = payResponse.getBody();
        } catch (AlipayApiException e) {
            throw new RuntimeException(e);
        }
        //5.将响应的表单数据写入
        response.setContentType("text/html;charset="+charset);
        response.getWriter().write(body);
        response.getWriter().flush();
        response.getWriter().close();
    }


    @SneakyThrows
    public static void main(String[] args) {
        QRCodeUtil qrCodeUtil = new QRCodeUtil();
        String qrCode = qrCodeUtil.createQRCode("http://10.101.21.7:56050//transaction/alipaytest", 300, 300);
        System.out.println(qrCode);
    }
}
